﻿using System;
using System.Collections.Generic;
using System.Text;
using DataTransferObject;
using System.Data; 
namespace DataAccessObject
{
    public class TaoDeTuDongDAO
    {
        private string _conn;
        private int _intMaDe;
        private List<clsChiTietBoDe> _DanhSachChiTiet;
        public TaoDeTuDongDAO(String conn)
        {
            this._conn = conn;
        }

        public int MaDe
        {
            set { _intMaDe = value; }
            get { return _intMaDe; }
        }
        public List<clsChiTietBoDe> DanhSachChiTiet
        {
            set { _DanhSachChiTiet = value; }
            get { return _DanhSachChiTiet; }
        }
        public List<clsChiTietBoDe> TaoChiTietDe(string sql)
        {
            clsKetNoi _KetNoi = new clsKetNoi(_conn);
            _KetNoi.OpenConnection();
            IDataReader reader = _KetNoi.ExcuteReader(sql);
            if (reader == null)
            {
                throw new DataAccessException("Dữ liệu không đáp ứng.");
            }
            else
            {
                while (reader.Read())
                {
                    clsChiTietBoDe ChiTiet = new clsChiTietBoDe();
                    ChiTiet.MaBoDe = _intMaDe;
                    ChiTiet.MaCauHoi = Convert.ToInt32(reader["MaCauHoi"]);
                    _DanhSachChiTiet.Add(ChiTiet);
                }
            }
            _KetNoi.CloseConnection();
            return _DanhSachChiTiet;
        }

        public string BuildingSQL(int socauhoi, int start, int end, int loaicauhoi, int MaChuyenMuc)
        {
            string sql = @"SELECT top " + socauhoi + @" MaCauHoi FROM 
                (SELECT 
                   ROW_NUMBER() OVER (order by MaCauHoi) AS RowNumber, MacauHoi
                   FROM CauHoi Where LoaiCauHoi=" + loaicauhoi + " and MaChuyenMuc=" + MaChuyenMuc + @"
                ) _CauHoi
             WHERE RowNumber>=" + start + " And RowNumber <=" + end + " order by NewId()";
            return sql;
        }

    }
}
